<template>
  <div class="login">
    <div class="box">
      <div class="img">
        <img src="../assets/logo.png" alt="" />
      </div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from "../http/user.js";
export default {
  name: "",
  components: {},
  data() {
    return {
      ruleForm: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" },
        ],
      },
    };
  },
  created() {},
  mounted() {},
  methods: {
    submitForm(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          this.$store.dispatch("user/loginFn", this.ruleForm).then(
            () => {
              this.$router.push('/home');
            },
            () => {
              this.resetForm("ruleForm");
            }
          );
        } else {
         this.resetForm("ruleForm");
          return false;
        }
      });
    },
    // login(this.ruleForm).then((res) => {
    //   console.log(res);
    //   if (res.meta.status == 200) {
    //     localStorage.setItem("houtaitoken", res.data.token);
    //     localStorage.setItem("username", this.ruleForm.username);
    //     this.$router.push("/home");
    //     this.$message({
    //       message: "登录成功",
    //       type: "success",
    //     });
    //   } else {
    //     this.$message.error("用户名或者密码错误");
    //   }
    // });

    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  beforeEach(to, next, from) {
    if (localStorage.getItem("houtaitoken")) {
      next("/home");
    } else {
      next();
    }
  },
};
</script>
<style scoped lang='scss'>
.login {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #2b4b6b;
  .box {
    display: flex;
    justify-content: center;
    position: relative;
    width: 450px;
    height: 300px;
    box-shadow: 0 0 10px #ddd;
    background-color: #fff;
    text-align: center;
    .img {
      position: absolute;
      top: -85px;
      left: 150px;
      width: 160px;
      height: 160px;
      box-shadow: 0 0 10px #ddd;
      border: 10px solid #fff;
      background-color: #efefef;
      border-radius: 50%;
      img {
        width: 150px;
        height: 150px;
        border-radius: 50%;
      }
    }
  }
  .demo-ruleForm {
    margin-top: 100px;
    text-align: center;
  }
}
</style>

